package cn.com.infcn.controller;

import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.infcn.po.Restful;
import cn.com.infcn.util.mss.MssDataApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 创建索引api
 *
 */
@Controller
@RequestMapping
@Api(value = "ES数据添加修改删除借口", description = "ES数据添加修改删除借口!",position=1)
public class MssDataApiController {

	@Autowired
	private MssDataApi mssDataApi;

	/**
	 * 创建索引,
	 *
	 * @param name
	 *            库表名称
	 * @param jsonStr
	 *            对象json格式
	 * @param skiperr
	 *            是否跳过错误
	 * @param operation
	 *            操作类型(默认是replace): replace(覆盖), update(更新)
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	@RequestMapping("/api/MssDataApi/insert")
	@ResponseBody
	@ApiOperation(value = "根据文件ID获取文件信息", httpMethod = "POST", notes = "根据文件ID获取文件信息", code=12)
	public Object insert(@RequestParam(name = "name") String name, @RequestParam(name = "jsonStr") String jsonStr,
			@RequestParam(name = "skiperr", defaultValue = "true") boolean skiperr,
			@RequestParam(name = "operation", defaultValue = "replace") String operation) {

		Object insert = null;
		try {
			insert = mssDataApi.insert(name, jsonStr, skiperr, operation);
		} catch (Exception e) {
			e.printStackTrace();
			insert = Restful.instance(false, e.getMessage());
		}
		return insert;
	}

	/**
	 * 根据id删除某条数据
	 *
	 * @param name
	 *            库表名称
	 * @param id
	 *            数据id
	 * @return @
	 */
	@RequestMapping("/api/MssDataApi/delete")
	@ResponseBody
	@ApiOperation(value = "根据文件ID删除", httpMethod = "POST", notes = "根据文件ID删除", code=11)
	public Object delete(String name, String id) {
		Object delete = mssDataApi.delete(name, id);
		return delete;
	}
}
